// 
// DockablePanelNode.cs
//  
// Author:
//       Tomas Vondracek <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2010 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using Mono.Addins;
using Common.Gui.GuiWinFormsLib.Controls;
using Common.Interfaces;
using System.Reflection;

namespace Common.Gui.GuiWinFormsLib.ExtensionNodes
{
	
	[ExtensionNode("DockablePanel")]
	internal class DockablePanelFactoryNode : AbstractDockablePanelNode
	{
		const int DefaultPanelHeight = 16;
		
		/// <summary>
		/// the text displayed in dockable panel control panel as a title
		/// </summary>
		[NodeAttribute("title", typeof(String), true, "the text displayed in dockable panel control panel as a title")]
		string title;
		
		/// <summary>
		/// the class with the control that will be embedded into the docking container
		/// </summary>
		[NodeAttribute("contentType", typeof(/*System.Windows.Forms.Control*/String), true, 
					   "the class with the control that will be embedded into the docking container")]
		string contentType;
		
		/// <summary>
		/// determine which container will be panel placed in
		/// </summary>
		/// <remarks>
		/// Bottom, Center, Right, Left
		/// </remarks>
		[NodeAttribute("location", typeof(String), false, "Bottom, Center, Right, Left")]
		String location;
		
		/// <summary>
		/// if true, the panel will be able to detach from the main form and float
		/// </summary>
		[NodeAttribute("enableFloating", typeof(Boolean), false, "true to make panel floating")]
		bool enableFloating;	
		
		/// <summary>
		/// if true, the panel will contain the control bar
		/// </summary>
		[NodeAttribute("showControlBar", typeof(Boolean), false, "true to show control bar")]
		bool showControlBar;	
		
		internal override IDockablePanel GetDockablePanel()
		{
			IDockablePanel panel = null;
			if (enableFloating)
				panel = new FloatingDockablePanel();
			else
				panel = new DockablePanel();
			
			panel.TitleText = this.title;
			
			
			panel.Alignment = PanelAlignment.HorizontalBottom;
			switch (this.location.ToLower()) 
			{
			case "bottom":
				this.panelLocation = PanelLocation.Bottom;
				break;
			case "left":
				this.panelLocation = PanelLocation.Left;
				break;
			case "right":
				this.panelLocation = PanelLocation.Right;
				break;
			case "center":
				this.panelLocation = PanelLocation.Center;
				break;
			default:
				Logging.Logger.AddWarning("Unknown panel location " + this.location);
				this.panelLocation = PanelLocation.Center;
			break;
			}
			
			System.Windows.Forms.Control control = null;
			if (!String.IsNullOrEmpty(this.contentType))
			{
				try
				{
					var obj = Addin.CreateInstance(this.contentType);
					var factory = obj as IControlFactory;
					control = factory.Create();

					(panel as DockablePanel).AddContent(control);

					ContentManager.RegisterContent(panel.TitleText, control);
				}
				catch (Exception ex)
				{
					Logging.Logger.AddException(ex);
				}
			}
			else
			{
				Logging.Logger.AddError(string.Format("Can't add the content type {0} to the dockable panel {1}",
													  this.contentType, this.title));
			}
			
			//TODO get rid of the cast
			(panel as DockablePanel).Dock = System.Windows.Forms.DockStyle.Fill;
			(panel as DockablePanel).ShowControlBar = this.showControlBar;
			(panel as DockablePanel).ControlsBarHeight = DefaultPanelHeight;
			return panel;
		}
	}
}

